package com.tiancheng.trade.settlement.controller.admin;

import com.tiancheng.trade.commom.core.data.Result;
import com.tiancheng.trade.settlement.exception.BusinessException;
import com.tiancheng.trade.settlement.model.settlement.StatementCost;
import com.tiancheng.trade.settlement.service.settlement.IStatementCostService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 收入成本报表
 *
 * @author lzh
 */
@RestController
@RequestMapping("/admin/cost")
@Validated
@Slf4j
public class StatementCostController {

    @Autowired
    IStatementCostService statementCostService;

    /**
     * 报表类型 1成本 2成本差异 3无合同 4补签合同
     * 查询收入成本报表信息
     *
     * @param date
     * @return
     */
    @GetMapping("/findStatementCostList")
    public Result findStatementCostList(String date, String costType) throws BusinessException {

        List<StatementCost> list = statementCostService.getStatementCostList(date, costType);
        if (CollectionUtils.isEmpty(list)) {
            return Result.success(0);
        }
        return Result.success(list);
    }

    /**
     * 报表类型 1成本 2成本差异 3无合同 4补签合同
     * 更新收入成本报表信息
     *
     * @param date
     * @return
     */
    @GetMapping("/update")
    public Result update(String date, String costType) throws BusinessException {
        int data = statementCostService.updateStatementCost(date, costType);
        return Result.success("操作成功");
    }

    /**
     * 收入成本报表生成PDF文件
     *
     * @param date
     * @param costType
     * @return
     */
    @GetMapping("/statementCostPdf")
    public Result statementCostPdf(String date, String costType, HttpServletRequest request, HttpServletResponse response) throws Exception {
        Assert.isTrue(date != null || costType != null, "参数错误~");
        statementCostService.statementCostPdf(date, costType, request, response);
        return Result.success("导出成功");
    }

}
